const { reactive } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage } = ElementPlus

export function useDocumentView() {
  const baseAPI = '../../../Ashx/ContainerStepWorkReportVue3PageHandler.ashx';
  const documentViewObj = reactive({
    dialogVisible: false,//
    treeData: [],//文档查看Tree数据
    documentList: [],//文档查看文件清单
  });

  // 获取工艺信息
  const getDocumentViewTreeData = async configObj => {
    const { currentSelectContainer } = configObj;
    if (!currentSelectContainer.productid) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }
    documentViewObj.dialogVisible = true;
    documentViewObj.documentList = [];//清空文档清单
    const { productid, workflowid } = currentSelectContainer;
    const { data } = await useFetch({
      url: `${baseAPI}?actions=getDocumentViewTreeData`,
      data: { type: 'getDocumentViewTreeData', productid, workflowid }
    });
    documentViewObj.treeData = data.value;
  };

  // 获取文档list
  const getDocumentList = async node => {
    const { id, type } = node;
    const { data } = await useFetch({
      url: `${baseAPI}?actions=getDocumentList`,
      data: { type: `getDocumentList`, id, nodeType: type }
    });
    if (data.value.length === 0) {
      documentViewObj.documentList = [];
      return ElMessage({ type: 'error', message: `未查询到文档信息`, grouping: true })
    }
    documentViewObj.documentList = data.value;
  }

  // 文档下载
  const downloadFile = async filepath => {
    const { data } = await useFetch({
      url: `${baseAPI}?actions=getDownloadUrl`,
      data: { type: `getDownloadUrl`, filepath }
    });
    window.open(data.value);
  }

  return { documentViewObj, getDocumentViewTreeData, getDocumentList, downloadFile };
}